<template>
  <div class="postlist">
    <el-pagination
      background
      layout=" total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="pageSize"
      :total="total"
      style="margin: 8px 0px 0px 600px;"
    ></el-pagination>

    <el-table :data="postListData" style="width: 90%" class="postlist_table">
      <!-- 1、文章ID -->
      <el-table-column label="文章ID" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <!-- 2、创建日期 -->
      <el-table-column label="创建日期" width="180px">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left:10px">{{ (scope.row.create_date).split('T')[0] }}</span>
        </template>
      </el-table-column>

      <!-- 3、作者 -->
      <el-table-column label="作者" width="180px">
        <template slot-scope="scope">
          <i class="el-icon-s-custom"></i>
          <span style="margin-left:10px">{{ scope.row.user.nickname }}</span>
        </template>
      </el-table-column>

      <!-- 4、封面图 -->
      <el-table-column label="封面图" width="180px">
        <template slot-scope="scope">
          <img v-if="scope.row.cover[0]" :src="scope.row.cover[0].url | fixUrl" alt class="coverPic" />
          <img v-else src="@/assets/logo.png" alt class="coverPic" />
        </template>
      </el-table-column>

      <!-- 5、类型 -->
      <el-table-column label="类型" width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.type===1?'文章':'视频' }}</span>
        </template>
      </el-table-column>

      <!-- 6、操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="$router.push('/postpublish?id='+ scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout=" total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="pageSize"
      :total="total"
      style="margin: 8px 0px 0px 600px;"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      postListData: [],
      pageSize: 4,
      pageIndex: 1,
      total: 0
    };
  },

  created() {
    this.loadPage();
  },

  filters: {
    fixUrl(url){
      const reg = /^http/
      if(reg.test(url)){
        return url
      }else{
        return 'http://127.0.0.1:3000' + url
      }
    }
  },

  methods: {
    loadPage() {
      this.$axios({
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      }).then(res => {
        console.log("文章列表返回结果1", res);
        console.log("文章列表返回结果2", res.data.data);
        this.postListData = res.data.data;

        this.total = res.data.total;
      });
    },

    handleCurrentChange(val) {
      this.pageIndex = val;

      this.loadPage();
    },

    handleSizeChange(val) {
      this.pageSize = val;

      this.loadPage();
    }
  }
};
</script>

<style lang="less" scoped>
.postlist_table {
  margin: 26px 0 26px 0;
  padding: 30px 50px 30px;

  /deep/ .is-leaf {
    .cell {
      font-size: 16px;
      color: #333;
    }
  }

  /deep/ .el-table__row {
    .cell {
      font-weight: 600;
    }
  }

  .coverPic {
    width: 100px;
    height: 70px;
    object-fit: cover;
  }
}
</style>